package com.hemaapp.yjnh.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.ClientDetails;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.utils.ImageUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.util.XtomToastUtil;

/**
 * 类描述：
 * 创建人：GuiyouZhang
 * 创建时间：2016/7/18 17:08
 * 修改备注：
 */
public class MyFarmerActivity extends BaseActivity {
    private final static int REQUEST_EDIT = 0x01;//编辑个人资料

    @Bind(R.id.title_left_btn)
    ImageButton titleLeftBtn;
    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.iv_avatar)
    ImageView ivAvatar;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.tv_today_total)
    TextView tvTodayTotal;
    @Bind(R.id.tv_today_vistors)
    TextView tvTodayVistors;
    @Bind(R.id.tv_today_orders)
    TextView tvTodayOrders;
    @Bind(R.id.layout_my_release)
    LinearLayout layoutMyRelease;
    @Bind(R.id.layout_my_custom_management)
    LinearLayout layoutMyCustomManagement;
    @Bind(R.id.layout_my_goods_management)
    LinearLayout layoutMyGoodsManagement;
    @Bind(R.id.layout_my_custom_orders)
    LinearLayout layoutMyCustomOrders;
    @Bind(R.id.layout_my_goods_orders)
    LinearLayout layoutMyGoodsOrders;
    @Bind(R.id.ratingBar)
    RatingBar ratingBar;

    User user;
    private ClientDetails details;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_farmer);
        ButterKnife.bind(this);
        user = getApplicationContext().getUser();
        super.onCreate(savedInstanceState);
        getNetWorker().clientGet(user.getToken(), user.getId());
    }

    @Override
    protected void setListener() {
        titleRightBtn.setVisibility(View.GONE);
        titleText.setText("我的田家");
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CLIENT_GET:
                showProgressDialog("请稍候");
                break;
            default:
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CLIENT_GET:
                cancelProgressDialog();
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CLIENT_GET:
                HemaArrayResult<ClientDetails> uResult = (HemaArrayResult<ClientDetails>) baseResult;
                details = uResult.getObjects().get(0);
                setDatas();
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CLIENT_GET:
                XtomToastUtil.showShortToast(mContext, "获取信息失败," + baseResult.getMsg());
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CLIENT_GET:
                XtomToastUtil.showShortToast(mContext, "获取信息失败, 请稍候再试");
                break;
        }
    }

    private void setDatas() {
        ImageUtils.loadAvatar(mContext, details.getAvatar(), ivAvatar);
        tvName.setText(details.getNickname());
        tvAddress.setText(details.getAddress());
        String star = details.getStarscore();
        if (!TextUtils.isEmpty(star)) {
//            starMerchant.setStar(Integer.parseInt(star));//设置星星数量
            ratingBar.setRating(Float.parseFloat(details.getStarscore()));
        }
        tvTodayTotal.setText("¥"+details.getToday_bill_fee());
        tvTodayVistors.setText(details.getVisitor_count());
        tvTodayOrders.setText(details.getToday_bill_count());
    }

    @OnClick({R.id.title_left_btn, R.id.layout_farmer_info, R.id.layout_my_release,
            R.id.layout_my_custom_management, R.id.layout_my_goods_management,
            R.id.layout_my_custom_orders, R.id.layout_my_goods_orders})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.layout_farmer_info:
                //2016.10.24 start 和iOS统一去掉修改田家个人信息，如果需要修改，放开下面的注释
//                Intent itEdit = new Intent(mContext, ApplyFarmerActivity.class);
//                itEdit.putExtra("type", 2);
//                itEdit.putExtra("client", details);
//                startActivityForResult(itEdit, REQUEST_EDIT);
                //2016.10.24 end
                break;
            case R.id.layout_my_release:
                Intent itJournal = new Intent(mContext, JournalListActivity.class);
                startActivity(itJournal);
                break;
            case R.id.layout_my_custom_management:
                Intent itCustomize = new Intent(mContext, ManagerCustomizeActivity.class);
                startActivity(itCustomize);
                break;
            case R.id.layout_my_goods_management:
                Intent itManager = new Intent(mContext, ManagerGoodsActivity.class);
                startActivity(itManager);
                break;
            case R.id.layout_my_custom_orders:
                Intent itCustom = new Intent(mContext, FarmerCustomOrdersListActivity.class);
                startActivity(itCustom);
                break;
            case R.id.layout_my_goods_orders:
                Intent itGoodsOrder = new Intent(mContext, FarmerOrdersListActivity.class);
                startActivity(itGoodsOrder);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (Activity.RESULT_OK != resultCode) {
            return;
        }
        switch (requestCode) {
            case REQUEST_EDIT:
                getNetWorker().clientGet(user.getToken(), user.getId());
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
